package com.neusoft.gene.nep.role.service.impl;

import com.neusoft.gene.nep.role.entity.Sysnode;
import com.neusoft.gene.nep.entity.CommonResult;
import com.neusoft.gene.nep.role.entity.Roles;
import com.neusoft.gene.nep.role.mapper.RolesMapper;
import com.neusoft.gene.nep.role.service.IRolesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: TODO  角色表 服务实现类
 * @author: 君醉
 * @date: 2024/6/19 16:11
*/

@Service
public class RolesServiceImpl extends ServiceImpl<RolesMapper, Roles> implements IRolesService {

    @Override
    public CommonResult< List<Roles>> listAll() {
        List<Roles> lrs =  getBaseMapper().selectList(null);
        return CommonResult.success(lrs);
    }

    @Override
    public CommonResult modRoleByid(Roles roles) {
        int i =  getBaseMapper().updateById(roles);
        return CommonResult.success(i);
    }

    @Override
    public CommonResult addRole(Roles roles) {
//        如果使用数据库表的ID自增，那么ID列必须为null
        roles.setId(null);
        int i =  getBaseMapper().insert(roles);
//        此处省略500行。。。。。。。。
        return CommonResult.success(roles.getId());
    }

    @Override
    public CommonResult modRole(Roles roles) {
        int i = getBaseMapper().updateById(roles);
        return CommonResult.success(i);
    }

    @Override
    public CommonResult selectSysnodesByRid(Integer rid) {
        List<Integer> list = getBaseMapper().selectSysnodesByRid(rid);
        return CommonResult.success(list);
    }

    @Override
    public CommonResult modRoleAuth(Integer rid, List<Integer> sysnodeids) {
        if(sysnodeids != null && sysnodeids.size() > 0){
//            删除角色和系统功能的关系表中的旧数据
            int i =   getBaseMapper().deleteRoleAuth(rid);
            i =  getBaseMapper().insertRoleAuth(rid,   sysnodeids);
            return  CommonResult.success(i);
        }
        return CommonResult.failed(555,"授权失败");
    }

    @Override
    public CommonResult selectMenuByRoleid(Integer roleid) {
        List<Sysnode> lss =  getBaseMapper().selectMenuByRoleid(roleid);
        return CommonResult.success(lss);
    }
}
